package online.westonline.com.westonline.adapter.messgae_notice;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easy.west.westclient.Constants;
import com.easy.west.westclient.api.login.bean.User;
import com.easy.west.westclient.api.message.bean.MessageBean;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.UserInfo;
import de.hdodenhof.circleimageview.CircleImageView;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.entry.PushDataBean;
import online.westonline.com.westonline.ui.MeetRoomStateActivity;
import online.westonline.com.westonline.utils.TimeUtil;
import online.westonline.com.westonline.utils.UiUtlis;

/**
 * 展现
 * Created by xiatao on 2018/8/30.
 */

public class MessageNoticeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    @BindView(R.id.tv_empty_desc)
    TextView tvEmptyDesc;
    private int EMPTY_TYPE = 0;
    private int NORMAL_TYPE = 1;
    private List<MessageBean> messageBeanList = new ArrayList<>();
    private onMessageClickListener clickListener;
    public void setMessageBeanList(List<MessageBean> messageBeanList) {
        this.messageBeanList = messageBeanList;
    }

    public void setClickListener(onMessageClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == EMPTY_TYPE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_view, parent, false);
            return new EmptyMessageViewHolder(view);
        } else if (viewType == NORMAL_TYPE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_notice_item, parent, false);
            return new MessageNoticeViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MessageNoticeViewHolder)
        {
            final MessageBean bean = messageBeanList.get(position);
            MessageNoticeViewHolder noticeViewHolder = (MessageNoticeViewHolder) holder;
            //获取用户实体

            getUserDetil(bean.getUserid(),noticeViewHolder);
            //设置相关
            noticeViewHolder.tvMessageType.setText(bean.getTitle());

            String time = bean.getCreated_time();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINESE);
            if (!TextUtils.isEmpty(time))
            {
                try {
                    time = TimeUtil.getLocalTime(dateFormat.parse(bean.getCreated_time()).getTime());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            noticeViewHolder.tvMessageTime.setText(time);
            if (bean.getIsImportant()==1)
            {
                //重要通知不予展示
                noticeViewHolder.itemView.setVisibility(View.GONE);
            }
            if (bean.getIsread()==1)
            {
                noticeViewHolder.tvMessageState.setText("已读");
                noticeViewHolder.tvMessageState.setTextColor(UiUtlis.getColor(R.color.Event_Green));
            }
            else
            {
                noticeViewHolder.tvMessageState.setText("未读");
            }

        }

    }

    @Override
    public int getItemViewType(int position) {
        if (messageBeanList.size() > 0) {
            return NORMAL_TYPE;
        } else {
            return EMPTY_TYPE;
        }

    }

    @Override
    public int getItemCount() {
        return messageBeanList == null ? 0 : messageBeanList.size();
    }

    /**
     * 获取用户详情
     * @param userid
     * @param noticeViewHolder
     */
    public void getUserDetil(final int userid, final MessageNoticeViewHolder noticeViewHolder) {
        RequestParams requestParams = new RequestParams(Constants.FIND_USERBYUID+userid);
        x.http().get(requestParams, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                User user = new Gson().fromJson(result,User.class);
                //请求头像
                JMessageClient.getUserInfo(user.getData().getAccess_token(), new GetUserInfoCallback() {
                    @Override
                    public void gotResult(int i, String s, UserInfo userInfo) {
                        if (i==0)
                        {
                            userInfo.getBigAvatarBitmap(new GetAvatarBitmapCallback() {
                                @Override
                                public void gotResult(int i, String s, Bitmap bitmap) {
                                    noticeViewHolder.imgFromUserAvator.setImageBitmap(bitmap);
                                }
                            });
                        }
                    }
                });
                //设置用户名
                noticeViewHolder.tvMessageUserName.setText(user.getData().getName());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 展示viewholder
     */
    class MessageNoticeViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_from_user_avator)
        CircleImageView imgFromUserAvator;
        @BindView(R.id.tv_message_user_name)
        TextView tvMessageUserName;
        @BindView(R.id.tv_message_type)
        TextView tvMessageType;
        @BindView(R.id.li_message_user)
        LinearLayout liMessageUser;
        @BindView(R.id.tv_message_time)
        TextView tvMessageTime;
        @BindView(R.id.tv_message_state)
        TextView tvMessageState;

        public MessageNoticeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null!=clickListener)
                    {
                        clickListener.click(getLayoutPosition(),messageBeanList.get(getLayoutPosition()));
                    }
                }
            });
        }
    }

    /**
     * 空视图viewholder
     */
    class EmptyMessageViewHolder extends RecyclerView.ViewHolder {
        public EmptyMessageViewHolder(View itemView) {
            super(itemView);
        }
    }

    /**
     * 供他人点击的事件
     */
    public interface onMessageClickListener
    {
        void click(int pos,MessageBean messageBean);
    }
}
